<template>
  <div>
    <div class="header_search_nav search_form">
      <div class="ipt_left">
        <el-form ref="queryForm" :model="queryParams" label-width="80px">
          <el-row>
            <el-col :span="6">
              <div class="timebox">
                <el-form-item label="名称" prop="cityName">
                  <el-input
                    v-model="queryParams.cityName"
                    class="w-100"
                    placeholder="请输入门店城市名称"
                  />
                </el-form-item>
              </div>
            </el-col>
          </el-row>
        </el-form>
      </div>

      <div class="btn_right">
        <button class="btn1" @click="handleQuery">筛选</button>
        <button class="btn2" @click="resetQuery">重置</button>
      </div>
    </div>
    <div class="table_card">
      <div class="table_card_top">
        <span class="solid_left"></span>
        <div class="table_card_title">门店城市</div>
        <div class="op_right">
          <button class="primary_button" @click="NewUser">新增城市</button>
        </div>
      </div>

      <div class="table_list">
        <el-table :data="StoreCityList" stripe style="width: 100%">
          <el-table-column prop="citySeq" label="城市排序"> </el-table-column>
          <el-table-column prop="cityName" label="城市名称"> </el-table-column>
          <el-table-column prop="createTime" label="创建时间">
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button size="mini" type="text" @click="edit(scope.row)"
                >编辑</el-button
              >

              <el-button size="mini" type="text" @click="DelUser(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <pagination
          :total="total"
          :page.sync="queryParams.pageNo"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </div>

      <!-- 新建类目金额 -->
      <el-dialog
        :visible.sync="categoryVisible"
        width="500px"
        :title="title"
        class="no-dialog-padding"
        @close="Canceluser"
      >
        <span>
          <div class="source_box">
            <el-form
              :model="CreatecityForm"
              :rules="CreatecityRules"
              ref="CreatecityForm"
              label-width="100px"
              class="demo-ruleForm"
            >
              <el-form-item label="城市排序" prop="citySeq">
                <el-input-number
                  v-model="CreatecityForm.citySeq"
                  :max="1000000"
                  :controls="false"
                  class="w-100 text-left"
                  :min="0"
                ></el-input-number>
              </el-form-item>
              <el-form-item label="城市名称" prop="cityName">
                <el-input v-model="CreatecityForm.cityName"></el-input>
              </el-form-item>

              <el-form-item label="城市编码" prop="cityCode" v-if="!CreatecityForm.cityCodeOld">
                <el-input
                  v-model="CreatecityForm.cityCode"
                ></el-input>
              </el-form-item>

              <el-form-item label="城市编码" prop="cityCode" v-if="!!CreatecityForm.cityCodeOld">
                <el-input
                  v-model="CreatecityForm.cityCode"
                  :disabled="true"
                ></el-input>
              </el-form-item>
            </el-form>
          </div>
        </span>
        <span slot="footer" class="dialog-footer">
          <el-button class="cancel_btn" @click="Canceluser">取 消</el-button>
          <el-button
            type="primary"
            @click="userSubmit"
            :loading="CreateClienLoading"
            >确 定</el-button
          >
        </span>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import {
  getStoreCity,
  CreateStoreCity,
  UpdateStoreCity,
  getStoreCityInfo,
  DeleteStoreCity,
} from "@/api/business/storecity";

import { checkEnglish } from "@/utils/regular";
export default {
  name: "EryuoaStoreCity",
  data() {
    return {
      StoreCityList: [], // 门店城市列表数据
      CreatecityForm: {
        cityCode: "",
        cityName: "",
        citySeq: "",
        cityCodeOld: "", // 旧城市编码
      },
      CreatecityRules: {
        cityCode: [{ required: true, message: "请输入城市编码" }],
        cityName: [{ required: true, message: "请输入城市名称" }],
        citySeq: [{ required: true, message: "请输入排序" }],
      },
      categoryVisible: false, // 新增
      loading: false,
      title: "",
      total: 0,
      CreateClienLoading: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
      },
    };
  },

  created() {
    this.getList();
  },

  mounted() {},

  methods: {
    CheckEnglish(rule, value, callback) {
      if (!checkEnglish(value) && !!value) {
        callback(new Error("只能输入英文字母"));
      } else {
        callback();
      }
    },
    // 获取门店城市详情
    getCategorys(cityCode) {
      getStoreCityInfo({
        cityCode: cityCode,
      })
        .then((res) => {
          const { code, data } = res;
          if (code === 0) {
            this.CreatecityForm = data;
            this.CreatecityForm.cityCodeOld = data.cityCodeOld;
          }
        })
        .catch((err) => {});
    },

    // 门店城市
    getList() {
      getStoreCity(this.queryParams)
        .then((res) => {
          this.loading = false;
          const { code, data } = res;
          if (code === 0) {
            this.StoreCityList = data.list;
            this.total = data.total;
          }
        })
        .catch((err) => {
          this.loading = false;
        });
    },

    Canceluser() {
      this.resetFormData("CreatecityForm");
      this.CreateClienLoading = false;
      this.$nextTick(() => {
        this.categoryVisible = false;
      });
    },

    // 筛选
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetFormData("queryForm");
      this.queryParams.categoryMoneyDay = "";
      this.queryParams.categoryMoneyMoon = "";
      this.handleQuery();
    },

    // 确定
    userSubmit() {
      this.$refs["CreatecityForm"].validate((valid) => {
        if (valid) {
          if (!!this.CreatecityForm.cityCodeOld) {
            this.UpdateCategorys();
          } else {
            this.CreateCategorys();
          }
        } else {
          return false;
        }
      });
    },

    // 新增
    CreateCategorys() {
      let CreatecityForm = JSON.parse(JSON.stringify(this.CreatecityForm));
      CreatecityForm.cityCodeOld = this.CreatecityForm.cityCodeOld;
      delete CreatecityForm.cityCodeOld;
      CreateStoreCity(CreatecityForm)
        .then((res) => {
          const { code } = res;
          if (code === 0) {
            this.Canceluser();
            this.$message.success("操作成功");
            this.getList();
          }
        })
        .catch((err) => {});
    },

    // 编辑
    UpdateCategorys() {
      let CreatecityForm = JSON.parse(JSON.stringify(this.CreatecityForm));
      CreatecityForm.cityCodeOld = this.CreatecityForm.cityCodeOld;
      UpdateStoreCity(CreatecityForm)
        .then((res) => {
          const { code } = res;
          if (code === 0) {
            this.Canceluser();
            this.$message.success("操作成功");
            this.getList();
          }
        })
        .catch((err) => {});
    },

    // 新建用户
    NewUser() {
      this.categoryVisible = true;
      this.title = "新增门店城市";
      this.CreatecityForm.cityCodeOld = "";
    },

    // 编辑类目
    edit(row) {
      this.categoryVisible = true;
      this.title = "编辑门店城市";
      this.getCategorys(row.cityCode);
    },

    // 表单重置
    resetFormData(formName) {
      this.$refs[formName].resetFields();
    },

    // 删除用户
    DelUser(row) {
      this.$confirm("确认要删除该门店城市吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          DeleteStoreCity({ cityCode: row.cityCode })
            .then((res) => {
              const { code } = res;
              if (code === 0) {
                this.getList();
                this.$modal.msgSuccess("操作成功");
              }
            })
            .catch(() => {});
        })
        .catch(() => {});
    },
  },
};
</script>
      
      <style lang="scss" scoped>
.w-200 {
  width: 200px;
  background: #ffffff;
  border-radius: 4px;
}

.client_name {
  font-size: 14px;
  font-weight: 400;
  color: #0052d9;
}

.give {
  height: 40px;
  font-size: 14px;
  font-weight: 400;
  color: rgba(0, 0, 0, 0.85);
  margin-top: 40px;
  line-height: 40px;
}

.el-form-item {
  margin-bottom: 20px;
}

.recharge_form {
  padding: 10px 24px;
}

.memberName {
  font-size: 14px;
  font-weight: 400;
  color: rgba(0, 0, 0, 0.85);
}
.el_row_item {
  margin-bottom: 10px;
}
.el_row_date {
  margin-top: 20px;
}
.el_row_reason {
  margin-top: 10px;
}

.el_row_date {
  .el-form-item {
    margin-bottom: 0px;
  }
}
.source_box {
  margin: 10px 24px;
}
.ipt_left {
  padding-right: 25px;
}
</style>
      
    
    